/*

=============================================
Additional information for A242046
=============================================

The content of this file demonstrates the
concatenation properties mentioned at
the comments for this sequence.

Note: You also can load this as a GP-PARI script.

Nested/concatenated terms. Patterns dictionary:

1) The patterns are named in lexicographic order
   of appearance.

2) The replacements should be made according the
   length of the patterns, shortest patterns first.
   
3) Define more patterns if necessary.

Given x=10 a positive integer (Specifically 10 for OEIS).

By replacement priority, the patterns are listed below:

D=x=10
E=(x-1)*x=90

A=D/D=1
B=(E-D)/D=8
C=E/D=9

{1}=DE
{2}=BCD
{3}=C{1}
{4}=E{2}
{5}=E{3}
{6}=E{4}
{7}={1}{4}
{8}=A{7}
{9}=BBE{5}
{10}=C{8}
{11}=E{9}
{12}=E{10}
{13}={1}{10}
{14}=A{13}
{15}=BBE{11}
{16}=C{14}
{17}=E{15}
{18}=E{16}
{19}={1}{16}
{20}=A{19}
{21}=BBE{17}
{22}=C{20}
{23}=D{22}
{24}=A{23}
{25}=A{24}
{26}=A{25}
{27}=BBBBE{21}
{28}=C{26}
{29}=E{27}
{30}=E{28}
{31}={1}{28}
{32}=A{31}
{33}=A{32}
{34}=A{33}
{35}=BBBBE{29}
{36}=C{34}
{37}=D{36}
{38}=A{37}
{39}=BBE{35}
{40}=C{38}
{41}=E{39}
{42}=E{40}
{43}={1}{40}
{44}=A{43}
{45}=BBE{41}
{46}=C{44}
{47}=D{46}
{48}=A{47}
{49}=A{48}
{50}=A{49}
{51}=BBBBE{45}
{52}=C{50}
{53}=A{52}
{54}=A{53}
{55}=A{54}
{56}=A{55}
{57}=BBBBE{51}
{58}=C{56}
{59}=E{57}
{60}=E{58}
{61}={1}{58}
{62}=A{61}
{63}=A{62}
{64}=A{63}
{65}=BBBBE{59}
{66}=C{64}
{67}=D{66}
{68}=A{67}
{69}=BBE{65}
{70}=C{68}
{71}=E{69}
{72}=E{70}
{73}={1}{70}
{74}=A{73}
{75}=A{74}
{76}=A{75}
{77}=BBBBE{71}
{78}=C{76}
{79}=D{78}
{80}=A{79}
{81}=BBE{77}
{82}=C{80}
{83}=D{82}
{84}=A{83}
{85}=A{84}
{86}=A{85}
{87}=BBBBE{81}
{88}=C{86}
{89}=D{88}
{90}=A{89}
{91}=A{90}
{92}=A{91}
{93}=A{92}
{94}=A{93}
{95}=BBBBBBE{87}
{96}=C{94}
{97}=D{96}
{98}=A{97}
{99}=BBE{95}
{100}=C{98}
{101}=E{99}
{102}=E{100}
{103}={1}{100}
{104}=A{103}
{105}=BBE{101}
{106}=C{104}
{107}=E{105}
{108}=E{106}
{109}={1}{106}
{110}=A{109}
{111}=BBE{107}
{112}=C{110}
{113}=D{112}
{114}=A{113}
{115}=A{114}
{116}=A{115}
{117}=A{116}
{118}=A{117}
{119}=A{118}
{120}=A{119}
{121}=A{120}
{122}=A{121}
{123}=A{122}
{124}=A{123}
{125}=BBBBBBBBBBBBE{111}
{126}=C{124}
{127}=D{126}
{128}=A{127}
{129}=BBE{125}
{130}=C{128}
{131}=D{130}
{132}=A{131}
{133}=A{132}
{134}=A{133}
{135}=BBBBE{129}
{136}=C{134}
{137}=E{135}
{138}=E{136}
{139}={1}{136}

...And so on.

For example, "{11}=E{9}" should be read as:

"Pattern #11 is (by definition) the concatenation of patterns E and #9".

The purpose is to encode any given sample composed with the first M terms of
this sequence in the form:

D, E, {1}, {2}, {3}, ...., {k}

This is, like a simple enumeration in increasing order of the defined patterns.

Question:
Which would be the semi-numerical algorithm underlying to this sequence,
based on this replacements scheme?

*/

ap(n,x)={my(s);forprime(p=1,n,s+=x^p);s}
a(n,x=10)={abs((x^(n+1)-1)/(x-1)-2*ap(n,x)-1)}

/* Extension: Aug 7 2014 */

av(n,x,y=x)=digits(a(n,x),y); /* Digits of a(n,x) "when it is expressed in base y" 
                               (Be careful!, perhaps it should be always y>=x) */
fixMe(i,B)=if(i>=B,B-1,i);

letter(i)=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"][fixMe(i,36)+1];

aStr(n,x=10,y=x)={my(s="",w=av(n,x,y));for(j=1,#w,s=Str(s,letter(w[j])));s}

/* Deletes leading character given by str1[1], str1[2], ... from  str2 (recursive version) */
leadingChrDel(str1,str2,index=1)={my(ans="",V0=Vecsmall("0"),V1=Vecsmall(str1),V2=Vecsmall(str2),j);while(V2[j++]==V1[index],);while(j<=#str2,ans=Str(ans,letter(V2[j]-V0[1]));j++);if(index==#str1,ans,leadingChrDel(str1,ans,index+1))};

/* ...That's all for now. - EOF */